//
//  SWSQliteManager.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/4/9.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit

class SWSQliteManager: NSObject {

    static let sheadManager = SWSQliteManager()
    
    let queue: FMDatabaseQueue
    
    override init() {
        //数据库保存路径
        let dbPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last?.stringByAppendingString("/status.db")
        print("daPath: \(dbPath)")
        
        queue = FMDatabaseQueue(path: dbPath)
        super.init()
        
        createTable("T_Status")
    }
    
    //创建表
    func createTable(tName: String){
        
        let sql = "CREATE TABLE IF NOT EXISTS \(tName) (status_id INTEGER NOT NULL PRIMARY KEY, status TEXT, user_id INTEGER, create_at DEFAULT (datetime('now', 'localtime')));"
        
        print("sql: \(sql)")
        
        queue.inDatabase { (db) -> Void in
            if db.executeUpdate(sql){
                print("创建表成功！");
            }
        }
    }
    
    
}
